package csu.web.mypetstore.web.servlet;
import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.service.CartService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "RemoveCartItemServlet", urlPatterns = {"/removeCartItem"})
public class RemoveCartItemServlet extends HttpServlet {
    private static final String ERROR_FORM = "/WEB-INF/jsp/common/error.jsp";
    private CartService cartService;
    public RemoveCartItemServlet()
    {
        cartService=new CartService();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        Account loginAccount=(Account) session.getAttribute("loginAccount");
        Cart cart=(Cart)session.getAttribute("cart");
        if(cart==null)
        {
            //弹出错误信息，购物车为空
            session.setAttribute("errorMsg","Cart is NULL");
            req.getRequestDispatcher(ERROR_FORM).forward(req,resp);
        }
        String workingItemId=req.getParameter("workingItemId");
        cartService.removeItem(loginAccount.getUsername(), workingItemId);

        session.setAttribute("action","removeItemFormCart");

        resp.sendRedirect("cartForm");

    }
}
